/**
You are attempting to solve a Coding Contract. You have 10 tries remaining, after which the contract will self-destruct.

How many different distinct ways can the number 147 be written as a sum of integers contained in the set:

[5,6,7,8,11,12,14,15]?

You may use each integer in the set zero or more times.

 */

const sample_0 = [5,6,7,8,11,12,14,15];

// let sum_0 = sample_0.reduce( function (a,b){return a+b;} );
// console.log(sum_0 );

// function arr_sum(arr)
// {
//     if(0==arr.length)return 0;
//     return arr.reduce( function (a,b){return a+b;} );
// }

// let list_samples = [];

// function recursion_build_sample(arr,key)
// {
//     if(arr_sum(arr) > 147)
//     {
//         return;
//     }
//     // else
//     {
//         arr.push(sample_0[key]);
//         if(arr_sum(arr) == 147)
//         {
//             list_samples.push(arr);
//         }
//         else
//         {
//             for(let i=0;i<sample_0.length;++i)
//             {
//                 recursion_build_sample(arr.slice(),i);
//             }
//         }
//     }
// }

// for(let i=0;i<sample_0.length;++i)
// {
//     recursion_build_sample([],i);
// }

// console.log("结果数:",list_samples.length);

// 依然用贪婪算法, 但是减少递归次数:
var count_solution=0;
/**
 * 这次就没有sample问题了, 不需要记录方案
 */
function recursion_sub_number(remain,index)
{
    let number_x = sample_0[index];
    for(let i=0;i<remain;++i){
        if(i*number_x < remain){
            recursion_sub_number(remain-i*number_x,index+1);
        }else if(i*number_x == remain){
            ++count_solution;
        }else if(i*number_x > remain){
            break;
        }
    }
}

recursion_sub_number(147,0);

console.log("solution:",count_solution)



